package com.mryx.demo.controller;
import com.google.gson.Gson;
import com.mryx.demo.pojo.*;
import org.springframework.data.redis.core.RedisTemplate;
import com.alibaba.fastjson.JSON;
import com.mryx.demo.service.CommodityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 商品模块前端接口
 */
@Api(tags = {"商品模块 访问路径：/commodity"})
@RequestMapping("/commodity")
@RestController
@CrossOrigin
public class CommodityController {

    @Resource(name = "commodityService")
    private CommodityService service;//业务逻辑层对象

    @Resource
    private RedisTemplate<String,String> redisTemplate;
    /**
     * 获取商品一级分类
     * @return  商品一级分类
     */
    @GetMapping("/showOLmenu")
    @ApiOperation(value = "获取商品一级分类",notes = "获取商品一级分类")
    @CrossOrigin
    public String getAllType(){
        return JSON.toJSONString(service.showOLmenu());
    }
    /**
     * 添加商品一级分类
     * @return  商品一级分类
     */
    @PostMapping("/addOneType")
    @ApiOperation(value = "增加商品一级分类",notes = "增加商品一级分类")
    public String addOneType(String name){
        return JSON.toJSONString(service.addOneType(name));
    }

    /**
     * 获取商品一级分类详情
     * @return  商品一级分类
     */
    @GetMapping("/getOneTypeByid/{cid}")
    @ApiOperation(value = "获取商品一级分类详情",notes = "获取商品一级分类详情")
    public String getOneTypeByid(@PathVariable Integer cid){
        return JSON.toJSONString(service.getOneTypeByid(cid));
    }

    /**
     * 修改商品一级分类
     * @return  商品一级分类
     */
    @PostMapping("/updOneType")
    @ApiOperation(value = "修改商品一级分类",notes = "修改商品一级分类")
    public String updOneType(String fclass){
        return JSON.toJSONString(service.updOneType(new Gson().fromJson(fclass, Classification.class)));
    }


    /**
     * 获得商品二级分类
     * @return 二级分类列表
     */
    @GetMapping("/showTLmenu")
    @ApiOperation(value = "获得商品二级分类",notes = "获得商品二级分类")
    public String showTLmenu(){
        return JSON.toJSONString(service.showTLmenu());
    }

    /**
     * 获得商品
     * @return 获得商品
     */
    @GetMapping("/showAllByClass{clid}")
    @ApiOperation(value = "获得商品",notes = "获得商品")
    public String showAllByClass(@PathVariable Integer clid){
        return JSON.toJSONString(service.showAllByClass(clid));
    }

    /**
     * 根据输入的名称查询商品
     * @param name 输入的商品名称
     * @return 根据输入的名称查询商品
     */
    @GetMapping("/search")
    @ApiOperation(value = "根据输入的名称查询商品",notes = "根据输入的名称查询商品")
    public String search(String name){
        return JSON.toJSONString(service.search(name));
    }

    /**
     * 根据id商品详情
     * @param cid 接收到的id
     * @return 要查询的商品详情
     */
    @GetMapping("/showAllById/{cid}")
    @ApiOperation(value = "根据id查询商品详情",notes = "根据id查询商品详情")
    public String showAllById(@PathVariable Integer cid){
        return JSON.toJSONString(service.showAllById(cid));
    }

    /**
     * 添加商品到购物车，使用redis缓存
     * @param cart
     * @param session
     * @return
     */
    @GetMapping("/insert")
    @ApiOperation(value = "存入购物车",notes = "存入购物车")
    public int insert(Cart cart, HttpSession session){
        Gson gson=new Gson();
        int res=service.insert(cart);
        redisTemplate.opsForValue().set("cart",gson.toJson(cart));
        return res;

    }

    /**
     * 删除或清空购物车
     * @param cartid
     * @return
     */
    //@GetMapping("/del{cartid}")
    //@PathVariable Integer cartid
    @GetMapping("/del")
    @ApiOperation(value = "购物车根据id删除",notes = "购物车根据id删除")
    public Boolean del(){
        //Integer res=service.del(cartid);
        Gson gson=new Gson();
        Boolean bool=redisTemplate.delete("cart");
        return bool;
    }

    /**
     * 修改购物车数据
     * @param cart
     * @return
     */
    @GetMapping("/update")
    @ApiOperation(value = "购物车修改",notes = "购物车修改")
    public Integer update(Cart cart){
        Integer res=service.update(cart);
        Gson gson=new Gson();
        redisTemplate.opsForValue().set("cart",gson.toJson(cart));
        return res;
    }

    /**
     * 查询购物车数据
     * @return
     */
    @GetMapping("/showAll")
    @ApiOperation(value = "购物车查询",notes = "购物车查询")
    public String showAll(){
        Gson gson=new Gson();
        String list = redisTemplate.opsForValue().get("cart");
        return JSON.toJSONString(list);
    }


    @PostMapping("/insertCommodity")
    @ApiOperation(value = "添加商品数据",notes = "添加商品数据")
    public Boolean insertCommodity(String commodityJson, String pictureJson){
        Commodity commodity = new Gson().fromJson(commodityJson,Commodity.class);
        Picture picture = new Picture();
        picture.setCid(commodity.getCid());
        picture.setRoute(pictureJson);
        Boolean bool=false;
        try {
            Integer res=service.insertCommodity(commodity);
            Integer rse=service.insertPicture(picture);
            bool=true;
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            return bool;
        }
    }
    @GetMapping("/delCommodity{cid}")
    @ApiOperation(value = "删除商品信息",notes = "删除商品信息")
    public Boolean delCommodity(@PathVariable Integer cid){
        Boolean bool=false;
        try {
            Integer res=service.delCommodity(cid);
            Integer rse=service.delPicture(cid);
            bool=true;
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            return bool;
        }
    }
    @PostMapping("/updateCommodity")
    @ApiOperation(value = "修改商品信息",notes = "修改商品信息")
    public Boolean updateCommodity(String commodityJson, String pictureJson){
        Commodity commodity = new Gson().fromJson(commodityJson,Commodity.class);
        Picture picture = new Picture();
        picture.setCid(commodity.getCid());
        picture.setRoute(pictureJson);
        Boolean bool=false;
        try {
            Integer res=service.updateCommodity(commodity);
            Integer rse=service.updatePicture(picture);
            bool=true;
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            return bool;
        }
    }
    @GetMapping("/showAllCommodity")
    @ApiOperation(value = "查询全部商品",notes = "查询全部商品信息")
    public String showAllCommodity(Integer currentPage){
        return JSON.toJSONString(service.showAllCommodity(currentPage,5));
    }
    @GetMapping("/showPictureById{cid}")
    @ApiOperation(value = "查询全部商品",notes = "查询全部商品信息")
    public String showPictureById(@PathVariable Integer cid){
        return JSON.toJSONString(service.showPictureById(cid));
    }

    @GetMapping("/showCompany")
    @ApiOperation(value = "查询单位数据",notes = "查询单位数据")
    public String showCompany(){
        return JSON.toJSONString(service.showCompany());
    }
}
